package event.money.tracker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import MTClient.ClientLogic;
import SQLManager.SQLManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class EditEvent extends Activity {
	
	private ListView mContactListView;  
	
    private EditText EV_Title;
    private EditText EV_Decription;
    
    private String phone_forSQL;
    private String event_members;
    
    private static ProgressDialog loadingpopup = null;
    
    //private ArrayList<Map<String,String>> globalList;
    
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.event_edit);
	    
	    // set current activity (need this for callback)
        ClientLogic.SetCurrentActivity(this);
	    
	    mContactListView = (ListView)findViewById(R.id.ContactListView_EditEvent);  
	    System.out.println("Get Bundle");
	    
	    Bundle b=this.getIntent().getExtras();
	    
	    
	    ArrayList<String> income_contact = b.getStringArrayList("flying_list");
	    
	    System.out.println("Got Bundle");
	    //Draw the contact listview
	    
	    //Create a Map to store ID/Contact/Namefor view
	    ArrayList<Map<String, String>> listitems = new ArrayList<Map<String,String>>();
	    
	    event_members = "";
	    
	    //Loop out the Phone number form last Activity
	    //And loop up it's details from SQL
	    if (income_contact == null){
	    	System.out.println("Array is Null");
	    }else{
	    	System.out.println("Array is notNull");
	    	
	    	for(int i=0; i < income_contact.size() ; i++){
	    		phone_forSQL = income_contact.get(i);
	    		System.out.println("Find Contact:"+phone_forSQL);
	    		
	    		SQLManager.GetInstance().open();
	    		Cursor contactItem = SQLManager.GetInstance().GetContactListItemWithPhoneNum(phone_forSQL);
	    		contactItem.moveToFirst();
	    		String phone = contactItem.getString(contactItem.getColumnIndex("PhoneNumber"));
	    		String id = contactItem.getString(contactItem.getColumnIndex("UserID"));
	    		String name = contactItem.getString(contactItem.getColumnIndex("UserName"));
	    		
	    		System.out.println("Details_ID:"+id);
	    		System.out.println("Details_Name:"+name);
	    		System.out.println("Details_Phone:"+phone);
	    		
	    		Map<String, String> map=new HashMap<String, String>();
				map.put("DrawingID", id);
				map.put("DrawingName", name);
				map.put("DrawingPhone", phone);
				//map.put(I, value);
				listitems.add(map);
				
				// add to event members string for networking
				event_members += id + " ";
	    	}
	    	
	    		
	    }
	    
	    
	    //DrawSelectedContact();
	    SimpleAdapter listitemAdapter=new SimpleAdapter(this,
	    		listitems, //Array Data
                R.layout.contact_items_no_checkbox,
                new String[]{"DrawingName","DrawingPhone"}, //blinding key to XML item
                new int[]{R.id.ContactNameTV_noCheckbox,R.id.ContactPhoneTV_noCheckbox} //Target item
        );
	    
	    mContactListView.setAdapter(listitemAdapter);  
	    
	    //Define Editable View
	    EV_Title = (EditText) findViewById(R.id.Event_Title);
        EV_Decription = (EditText) findViewById(R.id.Event_Description);
	    
	    Button CancelBTN =(Button)findViewById(R.id.Event_Cancel);
	    Button SubmitBTN =(Button)findViewById(R.id.Event_SubmitServer);
	    
	    //
	    //
	    //Back to Main if Cancel Clicked
	    CancelBTN.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setResult(RESULT_OK);
		    	finish();
			}
		});
	    //
	    //
	    //Write SQL if Submit Clicked - then return to main view
	    SubmitBTN.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//Getback the content from UI
				String eventTitle = EV_Title.getText().toString();
				System.out.println("Ready to Submit Title:"+eventTitle);
	            String eventDescription = EV_Decription.getText().toString();
	            System.out.println("Ready to Submit Des:"+eventDescription);


				//Submit to server
	            loadingpopup = ProgressDialog.show(EditEvent.this, "", 
	                    "Loading. Please wait...", true);
	            if( !ClientLogic.GetInstance().SendCreateEventRequest( eventTitle, event_members + ClientLogic.GetUserID(), eventDescription ) ){
	            	handle_eventCreation( true );
	            }

			}
		});
	     
		
	}
	
	public void handle_eventCreation (boolean success) {
		
		loadingpopup.dismiss();
		
    	if(success) {
    		setResult(5);
    		finish();
    	} else
    		System.out.println("ERROR on event creation");
    }

}
